﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace SocketChat.Core.WinForm
{
    /// <summary>
    /// 专门用来显示消息的ListBox
    /// </summary>
    public class LogListBox : ListBox
    {
        public DateTime _LastAddMsgTime;
        public string _LastMsgName;

        public void AddMsg(string msg, string name, DrawType drawType) {
            AddMsg(new ListBoxItem { Time = DateTime.Now, Body = msg, Name = name, DrawType = drawType });
        }

        public void AddMsg(string msg, string name) {
            AddMsg(msg, name, DrawType.Msg);
        }

        public void AddMsg(ListBoxItem item) {
            var now = DateTime.Now;
            if ((now - _LastAddMsgTime).Seconds > 5 || _LastMsgName != item.Name) {
                this.Invoke(new Action(() =>
                {
                    this.Items.Add(new ListBoxItem { Body = item.Body, DrawType = DrawType.Head, Time = item.Time, Name = item.Name });
                }));
                _LastMsgName = item.Name;
            }
            this.Invoke(new Action(() =>
            {
                this.Items.Add(item);
                if (this.HorizontalExtent < item.Body.Length * 7.5)
                    this.HorizontalExtent = (int)(item.Body.Length * 7.5);
            }));
            _LastAddMsgTime = now;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public LogListBox() {
            _LastAddMsgTime = DateTime.Now.AddSeconds(-10);
            _LastMsgName = string.Empty;
            this.HorizontalScrollbar = true;
            this.DrawMode = DrawMode.OwnerDrawVariable;
            this.DrawItem += new DrawItemEventHandler(this.DrawItemHandler);
            this.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.MeasureItemHandler);
            this.Items.Add(string.Empty);
        }

        private void DrawItemHandler(object sender, DrawItemEventArgs e) {
            if (e.Index > 0) {
                var now = DateTime.Now;
                var item = this.Items[e.Index] as ListBoxItem;
                e.DrawBackground();
                e.DrawFocusRectangle();
                if (item != null) {
                    switch (item.DrawType) {
                        case DrawType.Msg:
                            e.Graphics.DrawString("        " + item.Body, new Font(new FontFamily("新宋体"), 9), new SolidBrush(Color.Black), e.Bounds);
                            break;
                        case DrawType.Head:
                            e.Graphics.DrawString(item.Name + " " + item.Time.ToLongTimeString(), new Font(new FontFamily("黑体"), 11), new SolidBrush(Color.Blue), e.Bounds);
                            break;
                        case DrawType.Error:
                            e.Graphics.DrawString("        " + item.Body, new Font(new FontFamily("黑体"), 11), new SolidBrush(Color.Red), e.Bounds);
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        private void MeasureItemHandler(object sender, MeasureItemEventArgs e) {
            if (e.Index > 0) {
                var item = this.Items[e.Index] as ListBoxItem;
                if (item != null) {
                    if (item.DrawType == DrawType.Head || item.DrawType == DrawType.Error)
                        e.ItemHeight = 17;
                    if (item.DrawType == DrawType.Msg)
                        e.ItemHeight = 14;
                }
            }
        }
    }
}
